package com.example.electronicmall.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Looper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.example.electronicmall.R;
import com.example.electronicmall.bean.EndMineOrder;
import com.example.electronicmall.bean.MineOrder;
import com.example.electronicmall.bean.OrderItemList;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

public class RoderAdapter extends RecyclerView.Adapter<RoderAdapter.BaseViewHolder> {

    List<Object> list;

    Context context;

    Bitmap bitmap;

    public RoderAdapter(List<Object> list, Context context) {
        this.list = list;
        this.context = context;
    }

    //自定义item的点击事件接口
    private OnRecyclerItemClick onRecyclerItemClick;

    public void setOnRecyclerItemClick(OnRecyclerItemClick onRecyclerItemClick) {
        this.onRecyclerItemClick = onRecyclerItemClick;
    }

    public interface OnRecyclerItemClick {
        void onItemChildClick(RoderAdapter adapter, View view, int position);
    }


    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        BaseViewHolder vh = null;
        switch (viewType) {
            case 0:
                return new TopHolder(inflate(parent, R.layout.start));
            case 1:
                return new BottomHolder(inflate(parent, R.layout.end));
            case 2:
                return new GoodsHolder(inflate(parent, R.layout.center));
        }
        return vh;
    }

    @Override
    public void onBindViewHolder(BaseViewHolder holder, int position) {
        holder.bindData(list.get(position), position);
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public int getItemViewType(int position) {
        Object content = list.get(position);
        if (content instanceof MineOrder) {
            return 0;
        }
        if (content instanceof OrderItemList) {
            return 2;
        }
        if (content instanceof EndMineOrder) {
            return 1;
        }

        return position;
    }

    private View inflate(ViewGroup parent, int layout) {
        return LayoutInflater.from(parent.getContext()).inflate(layout, parent, false);
    }

    public abstract class BaseViewHolder<DATA> extends RecyclerView.ViewHolder {
        public BaseViewHolder(View itemView) {
            super(itemView);
        }

        public abstract void bindData(DATA data, int position);
    }

    /**
     * top部分
     */
    public class TopHolder extends BaseViewHolder<MineOrder> {
        LinearLayout linearLayout;
        TextView orderSu;
        TextView startte;

        public TopHolder(View itemView) {
            super(itemView);
            linearLayout = itemView.findViewById(R.id.ist_ll);
            orderSu = itemView.findViewById(R.id.orderSu);
            startte = itemView.findViewById(R.id.startte);
        }

        @Override
        public void bindData(MineOrder mineOrder, int position) {
            orderSu.setText(mineOrder.getOrderSn());
            //holder.payAmount.setText(String.valueOf(list.get(position).getPayAmount()));
            //订单状态：0->待付款；1->待发货；2->已发货；3->已完成；4->已关闭；5->无效订单',
            int starus = mineOrder.getStatus();
            if (starus == 0) {
                startte.setText("等待付款");
            }
            if (starus == 1) {
                startte.setText("等待发货");
            }
            if (starus == 2) {
                startte.setText("等待收货");
            }
            if (starus == 3) {
                startte.setText("交易完成");
            }
            if (starus == 4) {
                startte.setText("交易关闭");
            }
        }
    }

    /**
     * Bottom部分
     */
    public class BottomHolder extends BaseViewHolder<EndMineOrder> {
        TextView payAmount;
        LinearLayout linearLayout;
        TextView orderQu;
        TextView li;

        public BottomHolder(View itemView) {
            super(itemView);
            linearLayout = itemView.findViewById(R.id.end_ll);
            payAmount = itemView.findViewById(R.id.payAmount);
            orderQu = itemView.findViewById(R.id.orderQu);
            li = itemView.findViewById(R.id.li);

        }

        @Override
        public void bindData(EndMineOrder endMineOrder, int position) {
            payAmount.setText(String.valueOf(endMineOrder.getPayAmount()));
            //订单状态：0->待付款；1->待发货；2->已发货；3->已完成；4->已关闭；5->无效订单',
            int starus = endMineOrder.getStatus();
            if (starus == 0) {
                li.setText("立即付款");
                orderQu.setText("取消订单");
            }
            if (starus == 1) {
                li.setText("催发货");
                orderQu.setVisibility(View.GONE);
            }
            if (starus == 2) {
                li.setText("确认收货");
                orderQu.setText("查看物流");
            }
            if (starus == 3) {
                li.setText("再次购买");
                orderQu.setText("待评价");
            }
            if (starus == 4) {
                li.setText("删除订单");
                orderQu.setText("再次购买");
            }
        }
    }

    /**
     * content部分
     */
    public class GoodsHolder extends BaseViewHolder<OrderItemList> {
        LinearLayout itemLL;
        TextView productPrice;
        TextView textcommodity;
        ImageView imageSU;

        public GoodsHolder(View itemView) {
            super(itemView);
            itemLL = itemView.findViewById(R.id.center_ll);
            productPrice = itemView.findViewById(R.id.productPrice);
            textcommodity = itemView.findViewById(R.id.textcommodity);
            imageSU = itemView.findViewById(R.id.imageSU);
        }

        @Override
        public void bindData(OrderItemList orderItemList, int position) {
            productPrice.setText(String.valueOf(orderItemList.getProductPrice()));
            textcommodity.setText(orderItemList.getProductName());

            new Thread(new Runnable() {
                @Override
                public void run() {
                    URL imageurl = null;
                    try {
                        imageurl = new URL(orderItemList.getProductPic());
                    } catch (MalformedURLException e) {
                        e.printStackTrace();
                    }
                    try {
                        HttpURLConnection conn = (HttpURLConnection) imageurl.openConnection();
                        conn.setDoInput(true);
                        conn.connect();
                        InputStream is = conn.getInputStream();
                        bitmap = BitmapFactory.decodeStream(is);
                        Handler handler = new Handler(Looper.getMainLooper());
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                imageSU.setImageBitmap(bitmap);
                            }
                        });
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }).start();

        }
    }
}
